import Vec2 from "./vec2";

class Rect {
  x = 0;
  y = 0;
  width = 0;
  height = 0;
  constructor(e, t, n, r) {
    let i = this;
    (i.x = isNaN(e) ? i.x : e),
      (i.y = isNaN(t) ? i.y : t),
      (i.width = isNaN(n) ? i.width : n),
      (i.height = isNaN(r) ? i.height : r);
  }
  isEmpty() {
    let e = this;
    return e.x === 0 && e.y === 0 && e.width === 0 && e.height === 0;
  }
  left(e?) {
    let t = this;
    return arguments.length > 0 && (t.x = e), t.x;
  }
  right(e?) {
    let t = this;
    return arguments.length > 0 && (t.width = e - t.x), t.x + t.width;
  }
  top(e?) {
    let t = this;
    return arguments.length > 0 && (t.y = e), t.y;
  }
  bottom(e?) {
    let t = this;
    return arguments.length > 0 && (t.height = e - t.y), t.y + t.height;
  }
  topLeft() {
    let t = this;
    return new Vec2(t.x, t.y);
  }
  topRight() {
    let t = this;
    return new Vec2(t.right(), t.top());
  }
  bottomRight() {
    let t = this;
    return new Vec2(t.right(), t.bottom());
  }
  bottomLeft() {
    let t = this;
    return new Vec2(t.left(), t.bottom());
  }
  equalsTo(e) {
    let t = this;
    return (
      t.x == e.x && t.y == e.y && t.width == e.width && t.height == e.height
    );
  }
  containsVec2(e) {
    let t = this;
    return (
      e.x >= t.left() && e.x <= t.right() && e.y >= t.top() && e.y <= t.bottom()
    );
  }
  containsRect(e) {
    let t = this;
    return (
      e.left() >= t.left() &&
      e.right() <= t.right() &&
      e.top() >= t.top() &&
      e.bottom() <= t.bottom()
    );
  }
  intersect(n) {
    let r = this,
      i = r.topLeft(),
      s = r.bottomRight(),
      o = n.topLeft(),
      u = n.bottomRight(),
      a = new Vec2(Math.max(i.x, o.x), Math.max(i.y, o.y)),
      f = new Vec2(Math.min(s.x, u.x), Math.min(s.y, u.y)),
      l = new Rect(0, 0, 0, 0);
    return (
      a.x <= f.x &&
        a.y <= f.y &&
        ((l.x = a.x),
        (l.y = a.y),
        (l.width = f.x - a.x),
        (l.height = f.y - a.y)),
      l
    );
  }
  shrink() {
    let e = this;
    (e.x = Math.ceil(e.x)),
      (e.y = Math.ceil(e.y)),
      (e.width = Math.floor(e.width)),
      (e.height = Math.floor(e.height));
  }
  expand() {
    let e = this;
    (e.x = Math.floor(e.x)),
      (e.y = Math.floor(e.y)),
      (e.width = Math.ceil(e.width)),
      (e.height = Math.ceil(e.height));
  }
  clone() {
    let e = this;
    return new Rect(e.x, e.y, e.width, e.height);
  }
  toString() {
    let e = this;
    return (
      "[x:" +
      e.x +
      ",y:" +
      e.y +
      ",width:" +
      e.width +
      ",height:" +
      e.height +
      "]"
    );
  }
}

export default Rect;
